package adminValid

import (
	"github.com/astaxie/beego/validation"
	"strings"
)

type LoginValidation struct{
	BaseValidation
	Username string `json:"username" valid:"Required"`
	Password string `json:"password" valid:"Required"`
}

func (v *LoginValidation) GetFiledName(key string) string{
	filed := make(map[string]string)
	filed["Username"] = "用户名"
	filed["Password"] = "密码"
	return filed[key]
}


// 如果你的 struct 实现了接口 validation.ValidFormer
// 当 StructTag 中的测试都成功时，将会执行 Valid 函数进行自定义验证
func (u *LoginValidation) Valid(v *validation.Validation) {
	if strings.Index(u.Username, "admin") != -1 {
		v.SetError("Username", "里不能含有 admin")
	}
}

func (v *LoginValidation) ValidatorAuto(data interface{}) (result bool, msg string){
	validation.SetDefaultMessage(v.getDefaultMessage())
	validate := validation.Validation{}
	isValid, err := validate.Valid(data)

	r := true
	m := ""
	if err != nil {
		r = false
		m = err.Error()
	}

	if !isValid {
		r = false
		m = v.GetFiledName(validate.Errors[0].Field) + validate.Errors[0].Message
	}
	return r, m
}
